<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<link rel="stylesheet" type="text/css" href="/static/component/laytp/module/cityPicker/city-picker.css">

<body id="optPage">
<form class="layui-form layui-form-pane" id="commentForm">
    <div class="page-title"><div class="page-title-con"><span>编辑材料出库</span><div class="move_title"></div><?php include "../public/sub.php";?></div></div>
    <div class="main-container">

        <div class="block-generic">


            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>基本信息</span></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>出库单号</label>
                        <div class="layui-input-block">
                            <input type="text" id="number" placeholder="请输入" disabled autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>项目名称</label>
                        <div class="layui-input-block">
                            <input type="text" readonly id="enginid_name" class="layui-input enginid disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>出库日期</label>
                        <div class="layui-input-block">
                            <input type="text" id="chudt" readonly placeholder="请选择" clearable  autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">领料人</label>
                        <div class="layui-input-block">
                            <input type="text" id="baner" disabled placeholder="请输入" clearable  autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                </div>
            </div>
        </div>



        <div class="block-generic">
            <div class="layui-form-item height_auto">
                <div class="short_table_list" key="1">
                    <div class="opt-btn-group">
                        <div><span>出库明细</span></div>
                    </div>
                    <table class="layui-table" lay-size="sm">
                        <thead>
                        <tr>
                            <th width="45">序号</th>
                            <th><text>*</text>物资名称及规格</th>
                            <th>单位</th>
                            <th>供应商</th>
                            <th>入库数量</th>
                            <th>累计出库数量</th>
                            <th>库存数量</th>
                            <th>本次出库数量</th>
                            <th>备注</th>
                        </tr>
                        </thead>
                        <tbody class="short_table_tbody" id="chu_list"></tbody>
                    </table>
                    <div class="listerror">提示：<span></span></div>
                </div>
            </div>
        </div>
    </div>
</form>
<script>
    var param_id = getQueryVariable('id') || 0;


    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;
        render_input_attr();

        facade.ajax({type: 'get', url: ask_stuff_mingxi_chu_edit, data:{id:param_id}}).done(function(res){
            if (res.code === 2000) {
                var data = res.data.data;
                var engininfo = res.data.engininfo;
                $("#number").val(data.number);
                $("#enginid_name").val(engininfo.name);
                $("#chudt").val(data.chudt);
                $("#baner").val(data.baner);


                var list = res.data.list;
                var str = '';
                str += '<tr>';
                str += '<td style="width: 45px">1</td>';
                str += '<td>';
                str += '<input type="text" id="'+list.id+'" placeholder="请选择" ruid="'+list.ruid+'" readonly class="layui-input name disabled" value="'+list.name+'">';
                str += '</td>';
                str += '<td><input type="text" readonly class="layui-input unit disabled" value="'+list.unit+'"></td>';
                str += '<td><input type="text" readonly class="layui-input gonger disabled" value="'+list.gonger+'"></td>';
                str += '<td><input type="text" readonly class="layui-input runum disabled" value="'+list.runum+'"></td>';
                str += '<td><input type="text" readonly class="layui-input chunum disabled" value="'+list.chunum+'"></td>';
                str += '<td><input type="text" readonly class="layui-input cunnum disabled" value="'+list.cunnum+'"></td>';
                str += '<td><input type="text" class="layui-input num" value="'+list.num+'"></td>';
                str += '<td><input type="text" class="layui-input explain" value="'+list.explain+'"></td>';
                str += '</tr>';
                $("#chu_list").html(str);
                render_from();
            }
        });


        form.on('select(type)', function(data){
            set_list_layout();
        });


        form.on('submit(add)', function(data){
            var list = [''];
            try {
                $('#chu_list tr').each(function (i) {
                    var id = $(this).find('.name').attr('id');
                    var num = $(this).find('.num').val();
                    var cunnum = $(this).find('.cunnum').val();
                    var explain = $(this).find('.explain').val();
                    var index = i + 1;

                    if(!num){
                        throw new Error('第'+index+'行，请输入本次出库数量');
                    }
                    if(num > cunnum){
                        throw new Error('第'+index+'行，本次出库数量不能大于库存数量');
                    }

                    list.push({
                        id:id,
                        num:num,
                        explain:explain,
                    });
                });
            } catch(e) {
                $(".listerror").find('span').text(e.message);
                $(".listerror").show();
                return false;
            }
            $(".listerror").hide();
            data.field.list = list;

            data = parseMoney(data);
            facade.ajax({url: ask_stuff_mingxi_chu_edit, data:data.field}).done(function(res){
                if (res.code === 2000) {
                    dataPostTimeOut();
                }
            });

            return false;
        });

    });









    //检测重复
    function check_chongfu(index, ruid){
        try {
            $('#chu_list tr').each(function (i) {
                var cur_ruid = $(this).find('.name').attr('ruid');
                if(index != i){
                    if(ruid == cur_ruid){
                        throw new Error(i+1);
                    }
                }
            });
        } catch(e) {
            return e.message;
        }

        return 0;
    }

</script>
</body>
</html>
